package com.yang.yangCommand;

import com.yang.YangRanks;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
import java.util.Map;

public class AddRankController implements CommandExecutor {
    private static YangRanks yangRanks;

    public AddRankController(YangRanks yangRanks) {
        this.yangRanks = yangRanks;
    }
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        //执行指令
        if(!label.equalsIgnoreCase("addRank")){
            return true;
        }
        if (args.length == 0){
            return true;
        }
        // 判断是否是玩家执行
        if (!(sender instanceof Player)) {
            return true;
        }
        // 获取加入团队的玩家名字
        String name = sender.getName();
        // 拿到团队名字
        String rankName = args[0];
        // 判断这个队伍是否存在
        Map<String, List<String>> rankMap = CreateRankController.rankMap;
        List<String> playerInRanks = CreateRankController.playerInRanks;
        // 判断是否有队伍
        if(rankMap.isEmpty()){
            sender.sendMessage("还没有队伍哦~");
            return true;
        }

        for (String rankKeyName : rankMap.keySet()) {
            if (rankKeyName.equals(rankName)){
                // 判断玩家是否在团队中
                List<String> rankList = rankMap.get(rankKeyName);
                // 判断队伍是否有四个人了
                if (rankList.size() >= 4){
                    sender.sendMessage("队伍人数已满");
                }
                if (playerInRanks.contains(name)){
                    sender.sendMessage("你已经在这个队伍中了");
                    return true;
                }
                // 判断玩家是否在团队中
                rankList.add(name);
                playerInRanks.add(name);
                sender.sendMessage("加入队伍成功");
                return true;
            }else{
                sender.sendMessage("队伍不存在");
                return true;
            }
        }
        return false;
    }
}
